package com.rois.sample.CompReaction;

import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import com.rois.librois.libRoISComp.CommandControl;


public class Window {
	private static CommandControl cmd;
	JFrame mainwindow;
	BufferedImage image_back;
	BufferedImage image;
	BufferStrategy image_buffer;

	String back_image_file = "back.png";
	String default_image_file = "default.png";
	String current_image_file;
	String image_file_path;

	CommandQueue cmd_queue = new CommandQueue();
	class CommandQueue{
		private Vector<String> cmd_id_list = new Vector<String>();
		private Vector<String> next_image_file_list = new Vector<String>();
		private Vector<Boolean> cmd_exec_status_list = new Vector<Boolean>();
		int size;
		CommandQueue()
		{
			clear();
		}
		void clear()
		{
			cmd_id_list.clear();
			next_image_file_list.clear();
			cmd_exec_status_list.clear();
			size = 0;
		};
		int size()
		{
			return size;
		}
		void add(
			String cmd_id,
			String image_file
		)
		{
			cmd_id_list.add(cmd_id);
			next_image_file_list.add(image_file);
			cmd_exec_status_list.add(false);
			size++;
		}
		int get_index(
			String cmd_id
		)
		{
			int iRes = -1;
			iRes = cmd_id_list.indexOf(cmd_id);
			return iRes;
		}
		void remove(
			int index
		)
		{
			if(index<size)
			{
				cmd_id_list.remove(index);
				next_image_file_list.remove(index);
				cmd_exec_status_list.remove(index);
				size--;
			}
		}
		void remove_sent_message()
		{
			int temp_size = size;
			for(int i=size; i>0; i--)
			{
				if(cmd_exec_status_list.get(i-1))
				{
					cmd_id_list.remove(i-1);
					next_image_file_list.remove(i-1);
					cmd_exec_status_list.remove(i-1);
					temp_size--;
				}
			}
			size = temp_size;
		}
		void set_status(
			int index,
			boolean status
		)
		{
			if(index<size)
			{
				cmd_exec_status_list.set(index, status);
			}
		}
		boolean get_status(
			int index
		)
		{
			boolean bRes = true;
			if(index<size)
			{
				bRes = cmd_exec_status_list.get(index);
			}
			return bRes;
		}
		String get_id(
			int index
		)
		{
			String strRes = "";
			if(index<size)
			{
				strRes = cmd_id_list.get(index);
			}
			return strRes;
		}
		String get_image(
			int index
		)
		{
			String strRes = "";
			if(index<size)
			{
				strRes = next_image_file_list.get(index);
			}
			return strRes;
		}
	};

	public Window(
		CommandControl cmd_obj
	)
	{
		cmd = cmd_obj;
		current_image_file = default_image_file;

		File path = new File(".");
		String current_path = path.getAbsoluteFile().getParent()+File.separator;
		image_file_path = current_path + "/fig/";

		// create window
		mainwindow = new JFrame("Reaction Window");
		mainwindow.addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent we)
				{
					close();
				}
			}
		);
		mainwindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		mainwindow.setResizable(false);
		mainwindow.setVisible(true);
		// set window size
		Insets ins = mainwindow.getInsets();
		mainwindow.setSize(300 + ins.left + ins.right, 400 + ins.top + ins.bottom);
		// read image
		try {
			image_back = ImageIO.read( new File(image_file_path + back_image_file) );
			image = ImageIO.read( new File(image_file_path + default_image_file) );
		} catch (IOException e) {
			e.printStackTrace();
		}
		// create image buffer
		mainwindow.setIgnoreRepaint(true);
		mainwindow.createBufferStrategy(2);
		image_buffer = mainwindow.getBufferStrategy();

		//draw default image
		Graphics g = image_buffer.getDrawGraphics();
		g.translate(ins.left, ins.top);
		g.drawImage(image_back, 0, 0, mainwindow);
		g.drawImage(image, 0, 0, mainwindow);
		g.dispose();
		image_buffer.show();

		// create timer
		Timer timer = new Timer();
		timer.schedule(new RefreshTask(), 0, 100);
	}

	class RefreshTask extends TimerTask
	{
		@Override
		public void run()
		{
			Graphics g = image_buffer.getDrawGraphics();
			if(!image_buffer.contentsLost())
			{
				Insets ins = mainwindow.getInsets();
				g.translate(ins.left, ins.top);
				for(int i=0; i<cmd_queue.size(); i++)
				{
					if(!cmd_queue.get_status(i))
					{
						if(!cmd_queue.get_status(i))
						{
							if(!current_image_file.matches(cmd_queue.get_image(i)))
							{
								current_image_file = cmd_queue.get_image(i);
								try {
									image = ImageIO.read( new File(image_file_path + current_image_file) );
								} catch (IOException e) {
									e.printStackTrace();
								}
							}
						}
						cmd_queue.set_status(i, true);
						cmd.sendCompleted(cmd_queue.get_id(i), "ok");
						break;
					}
				}
				cmd_queue.remove_sent_message();
				g.drawImage(image_back, 0, 0, mainwindow);
				g.drawImage(image, 0, 0, mainwindow);
				image_buffer.show();
			}
			else
			{
				while(!image_buffer.contentsLost())
				{
					g = image_buffer.getDrawGraphics();
					g.dispose();
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			g.dispose();
		}
	}

	public void close()
	{
		cmd.stop();
		System.out.println("closed.");
		System.exit(0);
	}

	public void setImage(
		String command_id,
		String filename
	)
	{
		cmd_queue.add(command_id, filename);
	}

	public void setDefaultImage(
		String command_id
	)
	{
		cmd_queue.add(command_id, default_image_file);
	}
	public void setCurrentImage(
		String command_id
	)
	{
		cmd_queue.add(command_id, current_image_file);
	}
}
